home *** CD-ROM | disk | FTP | other *** search
- Path: HOPPER.ACM.ORG!news
- From: varnk@e62.diebold.com (Ken Varn)
- Newsgroups: comp.lang.c
- Subject: Re: What is wrong with this loop?
- Date: 19 Apr 1996 18:26:46 GMT
- Organization: Diebold
- Distribution: world
- Message-ID: <4l8lt7$d9h@HOPPER.ACM.ORG>
- References: <4l86la$1t9@uwm.edu>
- NNTP-Posting-Host: 199.218.232.47
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <4l86la$1t9@uwm.edu>, mardavuy@alpha2.csd.uwm.edu says...
- />
- />#include <stdio.h>
- />int main(void)
- />{
- /> int dia;
- />
- /> char cd;
- />
- /> ...
- />
- /> scanf("%c", &cd);
- /> while (cd != 'm' || cd ! 'f' || cd != 'o')
- /> {
- /> printf("Re-enter m, f, or o.\n");
- /> scanf(%c", &cd);
- /> }
- /> ...
- />}
- />
- />It works fine, except that it gives me the printf() twice.
-
-
- Try changing your scanf() as follows:
-
- scanf("%c ",&cd);
-
- This tells scanf() to read past any white space characters after the
- charadter read.
-
- The reason you were seeing the problem that you had is because when you
- press enter after typing the character, the \n is appended to the stdin
- buffer, but you have not read it out. The next call to scanf() tried to read
- a character out of the stdin buffer, but only saw the \n, so it returns
- immediately.
-
- The scanf() function has a lot of tricky idiosyncrasies that need to be
- understood in order to use it properly. You may want to review your compiler
- documentation on its implementation.
-
-